﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.Reflection;

namespace WinFormUIXMLParser
{
    public class XmlReadConfig
    {
        private string _file = "";
        public WindowType FormType = WindowType.PageUserControl;
        private XDocument _xmlDoc = null;
        private XElement _root = null;
        public XmlFormObject FormObj = new XmlFormObject();
        public List<XmlButtonObject> ListButtons = new List<XmlButtonObject>();
        public List<XmlButtonObject> ListBaseButtons = new List<XmlButtonObject>();
        public List<XmlLabelObject> ListLabels = new List<XmlLabelObject>();
        public List<XmlTextBoxObject> ListTextBoxs = new List<XmlTextBoxObject>();
        public List<XmlPictureBoxObject> ListPicBoxs = new List<XmlPictureBoxObject>();
 
        public XmlReadConfig(string file)
        {
            _file = file; 
        }

        public bool OnReading()
        {
            if (!OpenXmlDOC())
            {
                return false;
            }
            GetXmlObject("Form", out FormObj);
            GetXmlObject("BaseButtons", "Button", out ListBaseButtons);
            GetXmlObject("Buttons", "Button", out ListButtons);
            GetXmlObject("Labels", "Label", out ListLabels);
            GetXmlObject("PictureBoxs", "PictureBox", out ListPicBoxs);
            GetXmlObject("TextBoxs", "TextBox", out ListTextBoxs);
            return true;
        }

        private bool OpenXmlDOC()
        {
            _xmlDoc = XDocument.Load(_file);
            _root = _xmlDoc.Element("Windows");
            string typeVal = _root.Attribute("Type").Value.Trim();
            FormType = (WindowType)Enum.Parse(typeof(WindowType), typeVal);
            return true;
        }

        private bool GetXmlObject<T>(string parentName, string nodeName, out List<T> listOuts) where T : new()
        {
            listOuts = new List<T>();
            try
            {
                XElement parent = _root.Element(parentName);
                Type type = typeof(T);
                IEnumerable<XElement> elems = parent.Elements(nodeName);
                foreach (XElement e in elems)
                {
                    T temp = new T();
                    IEnumerable<XAttribute> atts = e.Attributes();
                    foreach (XAttribute att in atts)
                    {
                        string name = att.Name.ToString();
                        string value = att.Value.Trim();
                        PropertyInfo propertyInfo = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public);
                        if (propertyInfo != null)
                        {                           
                            if (propertyInfo.PropertyType == typeof(bool))
                            {
                                propertyInfo.SetValue(temp, StaticTypes.ConvertToBoolean(value, true), null); //给对应属性赋值
                            }
                            else
                            {
                                propertyInfo.SetValue(temp, value, null); //给对应属性赋值
                            }
                        }
                        else
                        {

                        }
                    }
                    listOuts.Add(temp);
                }
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        private bool GetXmlObject<T>(string nodeName, out T t) where T : new()
        {
            t = new T();
            try
            {           
                XElement node = _root.Element(nodeName);
                Type type = typeof(T);
                IEnumerable<XAttribute> atts = node.Attributes();
                foreach (XAttribute att in atts)
                {
                    string name = att.Name.ToString();
                    string value = att.Value.Trim();
                    PropertyInfo propertyInfo = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public);                  
                    if (propertyInfo != null)
                    {
                        if (propertyInfo.PropertyType == typeof(bool))
                        {
                            propertyInfo.SetValue(t, StaticTypes.ConvertToBoolean(value, true), null); //给对应属性赋值
                        }
                        else
                        {
                            propertyInfo.SetValue(t, value, null); //给对应属性赋值
                        }
                    }
                    else
                    {

                    }
                }
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }
}
